from bottle import Bottle, run, error, request, response, template, json_dumps, json_loads
from Controller.Home import Home
from Controller.Ecode import Ecode
import hashlib

app = Bottle()
ecode = Ecode()
home = Home()


@app.route("/api/get_role_info")
def getRoleInfo():
    role_id = request.params.get("role_id")
    if not role_id:
        ret = {'code': ecode.PARAMS_LOST, 'msg': ecode.getEcodeStr(ecode.PARAMS_LOST)}
        return home.responseToRequest(ret)

    role_info = home.getRoleInfo(role_id)
    ret = {'code': 0, 'data': role_info}
    return home.responseToRequest(ret)


@app.route("/api/get_user_info")
def getUserInfo():
    uid = request.params.get("uid")
    if not uid:
        ret = {'code': ecode.PARAMS_LOST, 'msg': ecode.getEcodeStr(ecode.PARAMS_LOST)}
        return home.responseToRequest(ret)

    user_info = home.getUserInfo(uid)
    ret = {'code': 0, 'data': user_info}
    return home.responseToRequest(ret)


@app.route("/api/get_current_zone")
def getCurrentZone():
    uid = request.params.get("uid")
    if not uid:
        ret = {'code': ecode.PARAMS_LOST, 'msg': ecode.getEcodeStr(ecode.PARAMS_LOST)}
        return home.responseToRequest(ret)
    current_zone = home.getCurrentZone(uid)
    ret = {'code': 0, 'data': current_zone}
    return home.responseToRequest(ret)


@app.route("/api/test")
def Test():
    if home.verifyAuth() == False:
        ret = {'code': ecode.FAILED, 'msg': ecode.getEcodeStr(ecode.FAILED)}
        return home.responseToRequest(ret)
    uid = request.params.get("uid")
    if not uid:
        ret = {'code': ecode.PARAMS_LOST, 'msg': ecode.getEcodeStr(ecode.PARAMS_LOST)}
        return home.responseToRequest(ret)

    ret = {'code': 0, 'data': 'success'}
    return home.responseToRequest(ret)


@app.error(404)
def error404(code):
    return '没找到页面！'


if __name__ == '__main__':
    run(app, host='localhost', port=8080, debug=True)
